package com.usian.service.impl;

import com.usian.config.RedisClient;
import com.usian.mapper.TbItemCatMapper;
import com.usian.pojo.*;
import com.usian.service.ItemCategoryService;
import com.usian.utils.AdNode;
import com.usian.utils.ItemCatData;
import com.usian.utils.ItemCatI;
import com.usian.utils.ItemCatResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class ItemCategoryServiceImpl implements ItemCategoryService {

    @Autowired
    private TbItemCatMapper tbItemCatMapper;

    @Value("${PROTAL_CATRESULT_KEY}")
    private String PROTAL_CATRESULT_KEY;

    @Autowired
    private RedisClient redisClient;


    @Override
    public List<TbItemCat> selectItemCategoryByParentId(Integer id) {
        TbItemCatExample example = new TbItemCatExample();
        TbItemCatExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo((long)id);
        criteria.andStatusEqualTo(1);
        List<TbItemCat> tbItemCats = tbItemCatMapper.selectByExample(example);
        return tbItemCats;
    }

    /**
     * 前台服务 商品分类菜单查询
     * @return
     */
    @Override
    public ItemCatResult selectItemCategoryAll() {
        /* 创建需要的实体类 */
        ItemCatResult itemCatResult = null;
        List<ItemCatData> resultList = new ArrayList<>();

        /* 首先先从redis中查询 */
        itemCatResult = (ItemCatResult)redisClient.get(PROTAL_CATRESULT_KEY);
        if (itemCatResult!=null){
            return itemCatResult;
        }
        itemCatResult = new ItemCatResult();

        /* 查询一级菜单目录 */
        List<TbItemCat> tbItemCats = findTbItemCatByParentId((long) 0);

        /* 根据一级目录查找二级目录 */
        for (TbItemCat tbItemCat : tbItemCats) {
            /* 查找所有二级目录 */
            List<TbItemCat> tbItemCatsTwo = findTbItemCatByParentId(tbItemCat.getId());

            /* 创建实体类并循环放入属性 */
            ItemCatData itemCatData = new ItemCatData();
            itemCatData.setN(tbItemCat.getName());
            List<ItemCatI> dataList = new ArrayList<>();

            for (TbItemCat itemCat : tbItemCatsTwo) {
                /* 查找三级目录 */
                List<TbItemCat> tbItemCatsThree = findTbItemCatByParentId(itemCat.getId());

                /* 创建实体类并循环放入属性 */
                ItemCatI itemCatI = new ItemCatI();
                itemCatI.setN(itemCat.getName());
                List<String> ilist = new ArrayList<>();
                for (TbItemCat cat : tbItemCatsThree) {
                    ilist.add(cat.getName());
                }
                itemCatI.setI(ilist);
                dataList.add(itemCatI);

            }
            itemCatData.setI(dataList);
            resultList.add(itemCatData);

        }
        itemCatResult.setData(resultList);

        /* 存到redis中 */
        redisClient.set(PROTAL_CATRESULT_KEY,itemCatResult);

        return itemCatResult;
    }



    /**
     * 抽取公共方法 根据父id查找商品分类
     * @param parentId
     * @return
     */
    private List<TbItemCat> findTbItemCatByParentId(Long parentId){
        TbItemCatExample example = new TbItemCatExample();
        TbItemCatExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        return tbItemCatMapper.selectByExample(example);
    }
}
